﻿using RBACDemo.Domain.Shared;
using Youshow.Ace.Domain.Models;

namespace RBACDemo.Domain.UserInfo;

/// <summary>
///  权限表
/// </summary>
public class ApiAuth : BaseModel<long>
{
    public string? GroupName { get; set; }

    public string? AreaName { get; set; }

    public string ControllerName { get; set; }

    public string ActionName { get; set; }

    public string? ActionDescription { get; set; }

    public string Method { get; set; }

    public string RoutePath { get; set; }

    public ApiAuthUpdateState Update(ApiAuth newApiAuth)
    {
        if (GroupName == newApiAuth.GroupName && ActionDescription == newApiAuth.ActionDescription)
        {
            return ApiAuthUpdateState.None;
        }
        else
        {
            GroupName = newApiAuth.GroupName;
            ActionDescription = newApiAuth.ActionDescription;
            return ApiAuthUpdateState.Update;
        }
    }

    public ApiAuthUpdateState CheckAndUpdate(List<ApiAuth> newApiAuths)
    {
        var newApiAuth = newApiAuths.FirstOrDefault(m => m.RoutePath == RoutePath &&
                                                         m.Method == Method &&
                                                         m.ControllerName == ControllerName &&
                                                         m.ActionName == ActionName);
        if (newApiAuth != null)
        {
            return Update(newApiAuth);
        }
        else
        {
            return ApiAuthUpdateState.Delete;
        }
    }
}